﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DXApplicationTangche.Utils
{
    class LogHelp
    {
        public static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");
        public static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror");
        private static object thisLock = new object();
        public static void WriteLog(string info, bool realWriteLog = true)
        {
            lock (thisLock)
            {
                if (realWriteLog)
                {
                    if (loginfo.IsInfoEnabled)
                    {
                        loginfo.Info(info);
                    }
                }
            }
        }

        public static void WriteLog(string info, Exception se, bool realWriteLog = true)
        {
            lock (thisLock)
            {
                if (realWriteLog)
                {
                    if (logerror.IsErrorEnabled)
                    {
                        logerror.Error(info, se);
                    }
                }
            }
        }
        /// <summary>
        /// 日志保留多少天
        /// </summary>
        /// <param name="logFilePath"></param>
        /// <param name="saveDays"></param>
        public static void SaveLogDays(string logFilePath, int saveDays)
        {
            lock (thisLock)
            {
                string[] logFileName = Directory.GetFiles(logFilePath);
                if (logFileName.Count() > saveDays)
                {
                    File.Delete(logFileName[0]);
                }
            }
        }
    }
}
